﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;

namespace EncryptionServer.COM
{
    public class WriteLogForCheck
    {
        static Mutex mutex = new Mutex();
        static string filePath = Environment.CurrentDirectory + @"\AccessDBRecords.log";

        public static bool CreateLogFile()
        {
            try
            {
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw ex;
            }
        }

        //public static int WriteLogFile(StringBuilder sb)
        //{
        //    int result = 0;
        //    StreamWriter sw = null;
        //    CreateLogFile();
        //    using (sw = new StreamWriter(filePath, true))
        //    {

        //        try
        //        {
        //            sw.WriteLine(sb.ToString());
        //            sw.Close();
        //            return result;
        //        }
        //        catch (Exception ex)
        //        {
        //            throw ex;
        //        }
        //        finally
        //        {
        //            sw.Close();
        //        }
        //    }



        //}
        public static int WriteLogFile(string strMsg)
        {
            mutex.WaitOne();
            int result = 0;
            StreamWriter sw = null;
            CreateLogFile();
            using (sw = new StreamWriter(filePath, true))
            {
                try
                {
                    sw.WriteLine(strMsg);
                    sw.Close();
                    return result;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sw.Close();
                    mutex.ReleaseMutex();
                }
            }
        }
    }
}
